<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>三列五行表格</title>
    <link rel="stylesheet" />
    <style>
      table {
        width: 100%;
        border-collapse: collapse;
        margin-bottom: 20px;
      }

      th,
      td {
        padding: 10px;
        text-align: left;
        border: 1px solid #ccc;
        background-color: #fff6f6;
      }
      button {
        background-color: rgb(252, 171, 155);
        border: 1px solid #999;
        width: 80px;
        height: 30px;
      }
      button:hover {
        background-color: rgb(255, 233, 233);
      }
    </style>
  </head>
  <body>
    <table>
      <thead>
        <tr>
          <th>
            <span><input type="checkbox" id="checkAll" /> 全选</span>
          </th>
          <th>第一列</th>
          <th>第二列</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><input type="checkbox" class="checkbox" /></td>
          <td>表单内容</td>
          <td>表单内容</td>
        </tr>
        <tr>
          <td><input type="checkbox" class="checkbox" /></td>
          <td>表单内容</td>
          <td>表单内容</td>
        </tr>
        <tr>
          <td><input type="checkbox" class="checkbox" /></td>
          <td>表单内容</td>
          <td>表单内容</td>
        </tr>
        <tr>
          <td><input type="checkbox" class="checkbox" /></td>
          <td>表单内容</td>
          <td>表单内容</td>
        </tr>
        <tr>
          <td><input type="checkbox" class="checkbox" /></td>
          <td>表单内容</td>
          <td>表单内容</td>
        </tr>
      </tbody>
    </table>
    <button>反选</button>

    <script>
      var checkAll = document.querySelector("#checkAll");
      var checkbox = document.querySelectorAll(".checkbox");

      // 全选
      checkAll.onclick = () => {
        for (var i = 0; i < checkbox.length; i++) {
          checkbox[i].checked = checkAll.checked;
        }
      };

      // 小复选全勾 全选自动选中
      for (var i = 0; i < checkbox.length; i++) {
        checkbox[i].addEventListener("click", () => {
          // 当被选中的复选框长度等于总小复选
          checkAll.checked =
            document.querySelectorAll(".checkbox:checked").length ==
            checkbox.length;
        });
      }

      // 反选
      var disCheck = document.querySelector("button");
      // console.log("🚀 ~ disCheck:", disCheck);
      disCheck.onclick = () => {
        checkbox.forEach((item) => {
          item.checked = !item.checked;
        });
        checkAll.checked =
          document.querySelectorAll(".checkbox:checked").length ==
          checkbox.length;
      };
    </script>
  </body>
</html>
